<!-- 编辑弹窗 -->
<template>
    <div class="ele-body">
      <!-- <el-card>
        <salecustomerDetailSearch @search="reload"  />
      </el-card> -->
      <el-card>
        <ele-pro-table ref="table"  row-key="id" :columns="warehouseItemColumns"  :datasource="datasource"  :need-page="true"  :cell-style="cellStyle" :height="tableHeight" :full-height="tableFullHeight">
          <template slot="outOrderNo" slot-scope="{ row }"  >
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.outOrderNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
  </template>
  <script>

  //初始化对像
  // import salecustomerDetailSearch from './salecustomer-detail-search.vue';
  import { removePageTab } from '@/utils/page-tab-util';
  import { QuerySalecustomerdetailSummaryViewByCondition } from '@/api/statistics';

  export default {
    name: 'CommissionDetail',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    // components:{salecustomerDetailSearch},
    data() {
      return {
        // 表单数据
        form: {},
        fixedHeight:true,
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        // 表格列配置
        warehouseItemColumns: [
          {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
          {prop: 'outOrderNo', label: '销售单号',slot:'outOrderNo',width: 150,  showOverflowTooltip: true,align: 'center',sortable:'outOrderNo' },
          // {prop: 'customerName', label: '客户', showOverflowTooltip: true,align: 'center' },
          {prop: 'orderAmount', label: '订单金额', width: 150, showOverflowTooltip: true, align: 'center',sortable:'orderAmount'},
          {prop: 'disAmount', label: '优惠金额',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'disAmount'},
          {prop: 'recAmount', label: '收款金额', width: 150, showOverflowTooltip: true, align: 'center',sortable:'recAmount'},
          {prop: 'currencyDesc', label: '币种',width: 100,  showOverflowTooltip: true, align: 'center'},
          {prop: 'preAmount', label: '预收款', width: 150, showOverflowTooltip: true, align: 'center',sortable:'preAmount'},
          {prop: 'surplusAmount', label: '未收金额',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'surplusAmount'},
          {
            prop: 'deliveryDate', label: '发货日期', width: 150, showOverflowTooltip: true, align: 'center',sortable: 'deliveryDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
          {
            prop: 'createDate', label: '制单日期', width: 150, showOverflowTooltip: true, align: 'center',sortable: 'createDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          }
        ],
        orderAmountTotal:0,
        disAmountTotal:0,
        recAmountTotal:0,
        preAmountTotal:0,
        surplusAmountTotal:0,
      };
    },
    computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 350px)' : void 0;
          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 250px)' : null;
          return fh;
        },
      },
    created(){
      this.customerId = this.$route.query.customerId;
    },
    methods: {

      /* 表格数据源 */
      async datasource(params) {

        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions)params.where.conditions = {};

        if(this.customerId){
          params.where.conditions.customerId =  this.customerId;
        }


        let data = await QuerySalecustomerdetailSummaryViewByCondition(params.where);


        this. orderAmountTotal =data.orderAmountTotal;
        this. disAmountTotal=data.disAmountTotal;
        this. recAmountTotal=data.recAmountTotal;
        this. preAmountTotal=data.preAmountTotal;
        this. surplusAmountTotal=data.surplusAmountTotal;
        
        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });

        return {
            list:data.salecustomerdetailView,
            count:data.totalrecords,
        };
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },

      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

      //单击单号
      clickbillNo(row){

        // 采购订单
        removePageTab({ key: '/businesshandling/saleoutmanger' });
        this.$nextTick(() => {
          this.$router.push({path: '/businesshandling/saleoutmanger', query: { "orderNo": row.outOrderNo } });
        });
      },
      /* 合计 */
      getSummaries(param){
        const sums = [];
        sums[0] = '合计' ;
        const { columns, data } = param;
        columns.forEach((column, index) => {
          if(index ==2){
            sums[index] =  Number(this.orderAmountTotal).toFixed(2);
          }else if(index ==3){
            sums[index] = Number(this.disAmountTotal).toFixed(4);
          }else if(index ==4){
            sums[index] = Number(this.recAmountTotal).toFixed(4);
          }else if(index ==6){
            sums[index] = Number(this.preAmountTotal).toFixed(4);
          }else if(index ==7){
            sums[index] = Number(this.surplusAmountTotal).toFixed(4);
          }
        });

        this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
        return sums;
      },
  
      /* 样式 */
      cellStyle(param){
        const { row, column, rowIndex, columnIndex } = param;
        if(columnIndex==3||columnIndex==4||columnIndex==2||columnIndex==7||columnIndex==6){
          return{
            color:''
          }
        }
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          //赋值this.form
          this.form = this.data;
          this.isUpdate = this.data.id !=null && this.data.id !=undefined;

          this.reload();
        } 
      }
    }
  };
  </script>